﻿using SQLite;

namespace DBHelpLib
{
    // All the code in this file is included in all platforms.
    public class dbHelpClass
    {
        /// <summary>
        /// 
        /// </summary>
        private static dbHelpClass dbHelp { get; set; }
        public SQLiteAsyncConnection Database;
        private dbHelpClass()
        {
            string path = Constants.DatabasePath;
            Database = new SQLiteAsyncConnection(path, Constants.Flags);
        }
        public static dbHelpClass GetDBHelp()
        {
            if (dbHelp == null)
            {
                dbHelp = new dbHelpClass();
            }
            return dbHelp;
        }
        public async Task Init<T>() where T : new()
        {
            var result = await Database.CreateTableAsync<T>();
        }

        //public async Task<List<TodoItem>> GetItemsAsync()
        //{
        //    await Init();
        //    return await Database.Table<TodoItem>().ToListAsync();
        //}

        //public async Task<List<TodoItem>> GetItemsNotDoneAsync()
        //{
        //    await Init();
        //    return await Database.Table<TodoItem>().Where(t => t.Done).ToListAsync();

        //    // SQL queries are also possible
        //    //return await Database.QueryAsync<TodoItem>("SELECT * FROM [TodoItem] WHERE [Done] = 0");
        //}

        //public async Task<TodoItem> GetItemAsync(int id)
        //{
        //    await Init();
        //    return await Database.Table<TodoItem>().Where(i => i.ID == id).FirstOrDefaultAsync();
        //}
        /// <summary>
        /// 保持数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <returns></returns>
        public async Task<int> SaveItemAsync<T>(T item)
        {
            return await Database.InsertAsync(item);
        }

        //public async Task<int> DeleteItemAsync(TodoItem item)
        //{
        //    await Init();
        //    return await Database.DeleteAsync(item);
        //}
    }
  
}
